package com.fp.inventory.filter;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class SetContentTypeFilter implements Filter {

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		Locale locale = null;

		Enumeration<?> values = ((HttpServletRequest) request)
				.getHeaders("accept-language");
		if (values.hasMoreElements()) {
			Enumeration<?> locales = request.getLocales();
			while (locales.hasMoreElements()) {
				locale = (Locale) locales.nextElement();
				break;
			}
		}

		if (locale == null) {
			locale = new Locale("", "");
		}

		ResourceBundle rb = ResourceBundle.getBundle("Resources", locale);

		String charset = rb.getString("charset");

		String contentType = "text/html; charset=" + charset;
		response.setContentType(contentType);

		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
